﻿#include "UCarousePage.h"
#include <QDateTime>
#include "UAlarm.h"
#include "Global.h"
UCarousePage::UCarousePage(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	m_pParent = (QWidget *)this->parent();
	m_isFullscreen = false;
	m_nIntervalMs = 10;
	m_nTimeIndex = 0;
	m_nIndex = 0;
	m_nChildCount = 0;
	m_pAlarmTech = new UAlarm(ui.pushButton_Process, 0);
	m_pAlarmFault = new UAlarm(ui.pushButton_Fault, 1);
	initUi();
	
}

UCarousePage::~UCarousePage()
{}

//初始化界面
void UCarousePage::initUi()
{
	connect(&m_timer, &QTimer::timeout, this, &UCarousePage::slot_startCarouse);
	connect(&m_timerDelete, &QTimer::timeout, this, &UCarousePage::slot_deleteCarousePage);
	installEventFilter(this);
	connect(ui.Btn_Left, SIGNAL(clicked()), this, SLOT(slot_left()));
	connect(ui.Btn_Right, SIGNAL(clicked()), this, SLOT(slot_right()));
	connect(ui.Btn_LeftLeft, SIGNAL(clicked()), this, SLOT(slot_leftLeft()));
	connect(ui.Btn_RightRight, SIGNAL(clicked()), this, SLOT(slot_rightRight()));
	connect(ui.Btn_Stop, SIGNAL(clicked()), this, SLOT(slot_start()));
	connect(ui.pushButton_close, SIGNAL(clicked()), this, SLOT(slot_enlarge()));
	connect(ui.pushButton_Process, SIGNAL(clicked()), this, SLOT(slot_alarmTech()));
	connect(ui.pushButton_Fault, SIGNAL(clicked()), this, SLOT(slot_alarmFalut()));
	ui.Btn_Left->setIcon(QIcon(":/SmartControl/Resource/Left.png"));              // 设置图标
	ui.Btn_LeftLeft->setIcon(QIcon(":/SmartControl/Resource/LeftLeft.png"));
	ui.Btn_Right->setIcon(QIcon(":/SmartControl/Resource/Right.png"));
	ui.Btn_RightRight->setIcon(QIcon(":/SmartControl/Resource/RightRight.png"));
	ui.Btn_Stop->setIcon(QIcon(":/SmartControl/Resource/run.png"));
	connect(ui.comboBox_Group, SIGNAL(currentIndexChanged(int)), this, SLOT(slot_comboxGroup(int)));
	connect(ui.comboBox_Card, SIGNAL(currentIndexChanged(int)), this, SLOT(slot_comboxCard(int)));
}

void UCarousePage::initCombox()
{
	if (g_pRootCarouse == nullptr)
	{
		return;
	}
	ui.comboBox_Group->blockSignals(true);   // 暂时关闭信号
	ui.comboBox_Card->blockSignals(true);   // 暂时关闭信号
	ui.comboBox_Group->clear();
	ui.comboBox_Card->clear();
	QVector<CBaseCarouse*>vecCarouse = g_pRootCarouse->getChildren();
	for (int i = 0; i < vecCarouse.size(); i++)
	{
		ui.comboBox_Group->addItem(vecCarouse[i]->getName());
		if (vecCarouse[i]->getName() == m_pNode->getName())
		{
			QVector<CBaseCarouse*>vecCarouseCard = vecCarouse[i]->getChildren();
			ui.comboBox_Group->setCurrentIndex(i);
			for (int j = 0; j < vecCarouseCard.size(); j++)
			{
				ui.comboBox_Card->addItem(vecCarouseCard[j]->getName());
			}
		}
	
	}
	ui.comboBox_Group->blockSignals(false);   // 暂时关闭信号
	ui.comboBox_Card->blockSignals(false);   // 暂时关闭信号
}

/**
* @ Author: Gaojia
* @ Description: 设置显示的节点或者页面
*/
void UCarousePage::setCurrNode(CBaseCarouse * pNode,bool bRefush)
{
	ui.label_Name->setText(g_pRoot->getName());
	/**
	* @ Author: Gaojia
	* @ Description: 点击的是节点
	*/
	if (pNode == m_pNode && !bRefush&& pNode->getChildren().size()== m_nChildCount)
	{
		return;
	}
	if (pNode->getType() == CBaseCarouse::enNodeCarouse|| pNode->getType()==CBaseCarouse::enRootCarouse)
	{
		m_pNode = (CNodeCarouse *)pNode;
		m_nIndex = 0;
		refushUiUi();
	}
	else  // 点击的是页面
	{
		/**
		* @ Author: Gaojia
		* @ Description:  获取点击页面的序号
		*/
		QVector<CBaseCarouse *>vecCarouse = ((CBaseCarouse *)pNode->getParent())->getChildren();
		int nNodeIndex = 0;
		for (int i = 0; i < vecCarouse.size(); i++)
		{
			if (vecCarouse[i]->getType() == CBaseCarouse::enPageCarouse)
			{
				if (vecCarouse[i] == pNode)
				{
					m_nIndex = nNodeIndex;
					break;
				}
				nNodeIndex++;
			}
		}

		//点击的页面的父节点和上次节点相同
		if (m_pNode == pNode->getParent())
		{
			setLabelText();
		}
		else
		{
			m_pNode = (CNodeCarouse *)pNode->getParent();
			refushUiUi();
		}	
	}
	m_nChildCount = m_pNode->getChildren().size();
}

/**
* @ Author: Gaojia
* @ Description: 设置轮播间隔
*/
void UCarousePage::setInterval(int nTime)
{
	m_nIntervalMs = nTime;
}

int UCarousePage::getInterval()
{
	return m_nIntervalMs;

}
/**
* @ Author: Gaojia
* @ Description: 开始或者停止轮播
*/
void UCarousePage::ctrCarouse(bool bWork)
{
	if (bWork)
	{
		if (!m_timer.isActive())
		{
			slot_start();
		}
	}
	else
	{
		if (m_timer.isActive())
		{
			slot_start();
		}
	}
	
}

/**
* @ Author: Gaojia
* @ Description: 全屏或者取消全屏
*/
void UCarousePage::enlargeUi(bool bEnlarge)
{
	if (m_isFullscreen == bEnlarge)
	{
		return;
	}
	else
	{
		slot_enlarge();
	}
}




void UCarousePage::refushUiUi()
{
	initCombox();
	/**
	* @ Author: Gaojia
	* @ Description: 刷新报警区的报警测点
	*/
	m_pAlarmTech->startRufush();
	m_pAlarmFault->startRufush();
	/**
	* @ Author: Gaojia
	* @ Description: 1.清空原来的界面
	*/
	if (m_timerDelete.isActive())
	{
		m_timerDelete.stop();
	}
	for (int i = 0; i < m_vecCarousePage.size(); i++)
	{
		m_vecCarousePage[i]->refushDate(false);
		//m_vecCarousePageDel.push_back(m_vecCarousePage[i]);
	}
	//启动延时释放的定时器
	if (m_vecCarousePageDel.size() > 0)
	{
		m_timerDelete.start(3000);
	}
	int count = ui.stackedWidget->count();
	for (int i = count - 1; i >= 0; --i)
	{
		QWidget* page = ui.stackedWidget->widget(i);
		ui.stackedWidget->removeWidget(page);
		//page->deleteLater(); // 安全删除页面，防止内存泄漏
	}
	m_vecCarousePage.clear();
	/**
	* @ Author: Gaojia
	* @ Description: 添加新界面
	*/
	QVector<CBaseCarouse *>vecChild = m_pNode->getChildren();
	for (int i = 0; i < vecChild.size(); i++)
	{
		//如果是界面节点就创建界面
		if (vecChild[i]->getType() == CBaseCarouse::enPageCarouse)
		{
			QString sPageName = vecChild[i]->getParent()->getName() + vecChild[i]->getName();
			UCarouseshowPage* pPage = nullptr;
			if (m_mapCarousePage.contains(sPageName))
			{
				pPage = m_mapCarousePage[sPageName];				
			}
			else
			{
				pPage = new UCarouseshowPage((CPageCarouse*)vecChild[i], this);
				m_mapCarousePage.insert(sPageName, pPage);
			}
			m_vecCarousePage.push_back(pPage);
			ui.stackedWidget->addWidget(pPage);
		}
	}
	setLabelText();
}

void UCarousePage::setLabelText()
{
	QDateTime current = QDateTime::currentDateTime();
	QString dateStr = current.date().toString("yyyy-MM-dd");
	QString timeStr = current.time().toString("HH:mm:ss");
	QString sPage =QString::number(m_nIntervalMs-m_nTimeIndex)+"    "+ QString::number(m_nIndex + 1) + "/" + QString::number(m_vecCarousePage.size())+A2T("页 ");
	ui.label->setText(sPage);
	ui.label_4->setText(dateStr + "\n" + timeStr);

	if (m_vecCarousePage.size() >= 1)
	{
		ui.stackedWidget->setCurrentIndex(m_nIndex);
	}

	//非当前显示页面全部停止刷新数据
	for (int i = 0; i < m_vecCarousePage.size(); i++)
	{
		int nCount = abs(i - m_nIndex);
		if (nCount ==0)
		{
			m_vecCarousePage[i]->refushDate(true);
		}
		else
		{
			m_vecCarousePage[i]->refushDate(false);
		}
	}
	
}

void UCarousePage::slot_left()
{
	slot_stop();
	if (m_vecCarousePage.size() <= 0)
	{
		return;
	}
	if (m_nIndex <= 0)
	{
		m_nIndex= m_vecCarousePage.size() - 1;
	}
	else
	{
		m_nIndex--;
	}
	ui.comboBox_Card->setCurrentIndex(m_nIndex);
}

void UCarousePage::slot_right()
{
	slot_stop();
	if (m_vecCarousePage.size() <= 0)
	{
		return;
	}
	if (m_nIndex >= (m_vecCarousePage.size()-1))
	{
		m_nIndex = 0;
	}
	else
	{
		m_nIndex++;
	}
	ui.comboBox_Card->setCurrentIndex(m_nIndex);
}

void UCarousePage::slot_leftLeft()
{
	slot_stop();
	if (m_vecCarousePage.size() <= 0)
	{
		return;
	}
	if (m_nIndex <= 0)
	{
		return;
	}
	m_nIndex = 0;
	ui.comboBox_Card->setCurrentIndex(m_nIndex);
}

void UCarousePage::slot_rightRight()
{
	slot_stop();
	if (m_vecCarousePage.size() <= 0)
	{
		return;
	}
	if (m_nIndex >= (m_vecCarousePage.size() - 1))
	{
		return;
	}
	m_nIndex = m_vecCarousePage.size() - 1;
	ui.comboBox_Card->setCurrentIndex(m_nIndex);
}

void UCarousePage::slot_start()
{
	if (!m_timer.isActive())
	{
		ui.Btn_Stop->setIcon(QIcon(":/SmartControl/Resource/stop.png"));  // 设置图标
		m_timer.start(1000);
	}	
	else
	{
		ui.Btn_Stop->setIcon(QIcon(":/SmartControl/Resource/run.png"));  // 设置图标
		m_timer.stop();
	}
}

void UCarousePage::slot_stop()
{
	if (m_timer.isActive())
	{
		ui.Btn_Stop->setIcon(QIcon(":/SmartControl/Resource/run.png"));  // 设置图标
		m_timer.stop();
	}
	m_nTimeIndex = 0;
}

bool UCarousePage::eventFilter(QObject* obj, QEvent* event)
{
	if (event->type() == QEvent::KeyPress) 
	{
		QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
		if (keyEvent->key() == Qt::Key_Escape) 
		{
			if(m_isFullscreen)
			slot_enlarge();
			return true; 
		}
	}
	return QWidget::eventFilter(obj, event); // 默认处理
}

void UCarousePage::slot_enlarge()
{
	if (m_isFullscreen)
	{
		bool bSure = showMessageBox(this, A2T("提示"), A2T("是否退出？"), 1, true);
		if (!bSure)
		{
			return;		
		}
		m_isFullscreen = false;		
	
	}
	else
	{
		m_isFullscreen = true;
	}
	emit sign_enlarge(m_isFullscreen);
}

void UCarousePage::slot_startCarouse()
{
	
	if (m_nTimeIndex >= m_nIntervalMs)
	{
		m_nTimeIndex = 0;
		if (m_vecCarousePage.size() <= 0)
		{
			return;
		}
		if (m_nIndex >= (m_vecCarousePage.size() - 1))
		{
			//屏蔽切换轮播组
			int nGroupIndex = ui.comboBox_Group->currentIndex() + 1;
			if (nGroupIndex >= ui.comboBox_Group->count())
			{
				nGroupIndex = 0;
			}
			slot_comboxGroup(nGroupIndex);
			m_nIndex = 0;
		}
		else
		{
			m_nIndex++;
			ui.comboBox_Card->blockSignals(true);   // 暂时关闭信号
			ui.comboBox_Card->setCurrentIndex(m_nIndex);
			ui.comboBox_Card->blockSignals(false);   // 恢复信号
		}
	}
	setLabelText();
	m_nTimeIndex++;
}

void UCarousePage::slot_alarmTech()
{
	m_pAlarmTech->show();
}

void UCarousePage::slot_alarmFalut()
{
	m_pAlarmFault->show();
}

void UCarousePage::slot_deleteCarousePage()
{
	m_timerDelete.stop();
	for (auto i = 0; i < m_vecCarousePageDel.size(); i++)
	{
		m_vecCarousePageDel[i]->deleteLater();
	}
	m_vecCarousePageDel.clear();	
}

void UCarousePage::slot_comboxGroup(int nIndex)
{
	if (g_pRootCarouse == nullptr)
	{
		return;
	}
	QVector<CBaseCarouse*>vecCarouse = g_pRootCarouse->getChildren();
	if (nIndex < vecCarouse.size())
	{
		setCurrNode(vecCarouse[nIndex], true);
	}
}

void UCarousePage::slot_comboxCard(int nIndex)
{
	slot_stop();
	if (m_vecCarousePage.size() <= 0)
	{
		return;
	}
	m_nIndex = nIndex;
	setLabelText();
}

